using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AandEDLL.DataAccess;
using AandEDLL;

namespace ASA
{
    public partial class AddItemDialog : BaseDialog
    {
        ResultsForm re;

        public AddItemDialog(ResultsForm RE)
        {
            re = RE;
            InitializeComponent();
        }

        private void AddItemDialog_Load(object sender, EventArgs e)
        {
            loadCategorydlg();
            dateTimePicker1.MaxDate = DateTime.Now.Date;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!IsValidData())
                return;
            bool saveSuccesss = true;
            try
            {
                Item newItem = new Item();
                if( ((Category)cboCategory.SelectedItem != null) )
                    newItem.CategoryID = ((Category)cboCategory.SelectedItem).CategoryID;
                newItem.URL = txtURL.Text;
                
                if (txtDescription.Text.Trim().Length > 0)
                    newItem.Description = txtDescription.Text;
                
                newItem.ItemName = txtName.Text;

                if (txtPrice.Text.Trim().Length > 0) // validation already done, but only add if price entered
                {
                    newItem.Price = Decimal.Parse(txtPrice.Text);
                    if(newItem.Price > Convert.ToDecimal(99999999.99))
                    {
                        MessageBox.Show(this, "Price entered cannot be greater than 99,9999,999.99", "ASA", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        saveSuccesss = false;
                        return;
                    }
                }

                newItem.SearchDate = dateTimePicker1.Value.Date;
                
                if (txtSource.Text.Trim().Length > 0)
                    newItem.Source = txtSource.Text;

                newItem.User = re.M_User;
                List<Item> newItems = new List<Item>();
                newItems.Add(newItem);
                Accessor.SaveItems(newItems);
                this.Close();
            }
            catch (Exception err)
            {
                MessageBox.Show(this, err.Message, "ASA", MessageBoxButtons.OK, MessageBoxIcon.Error);
                saveSuccesss = false;
            }
            if (saveSuccesss)
            {
                txtName.Text = "";
                txtDescription.Text = "";
                txtPrice.Text = "";
                txtSource.Text = "";
                txtURL.Text = "";
            }
            re.UpdateSavedItems();
        }

        private void loadCategorydlg()
        {
            cboCategory.Items.Clear();
            List<Category> categories = Accessor.getCategories();
            foreach (Category c in categories)
            {
                cboCategory.Items.Add(c);
            }
            cboCategory.ValueMember = "CategoryID";
            cboCategory.DisplayMember = "Name";

            //cboCategory.SelectedIndex = 0;
        }

        private bool IsValidData()
        {
            if (txtName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Please enter a name.", "Missing Required Field");
                return false;
            }

            try
            {
                if (txtPrice.Text.Trim().Length > 0)
                {
                    decimal price = Convert.ToDecimal(txtPrice.Text.Trim());
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Please enter a valid price.", "'Price' Field Invalid");
                return false;
            }

            return true;
        }

        private void btnAddEdit_Click(object sender, EventArgs e)
        {
            CategoryDialog categoryDialog = new CategoryDialog(true);
            
            if (categoryDialog.ShowDialog(this) == DialogResult.OK)
            {
                loadCategorydlg();
                cboCategory.SelectedIndex = cboCategory.FindStringExact(categoryDialog.SelectedCategory.Name);
            }
            
        }
    }
}